package com.bytezero.bytezero_weather.service;



import static com.bytezero.bytezero_weather.util.Utility.handleWeatherResponse;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;


import com.bytezero.bytezero_weather.gson.Weather;
import com.bytezero.bytezero_weather.util.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AutoUpdateService extends Service {
    public AutoUpdateService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
     return null;

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        updateWeather();
        updateBingPic();
        AlarmManager manager=(AlarmManager)getSystemService(ALARM_SERVICE);
        int anHour=4*60*60*1000;
        long triggerAtTime= SystemClock.elapsedRealtime()+anHour;
        Intent i=new Intent(this,AutoUpdateService.class);
        PendingIntent pi=PendingIntent.getService(this,0,i,0);
        manager.cancel(pi);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);

        return super.onStartCommand(intent, flags, startId);
    }

    private void updateBingPic() {
        SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
        String weatherString=prefs.getString("BytezeroWeather",null);
        if(weatherString!=null)
        {
            Weather weather= handleWeatherResponse(weatherString);
            String weatherId=weather.getHeWeather6().get(0).getBasicX().getCid();
            String weatherUrl="https://free-api.heweather.com/s6/weather?location="+weatherId.toString()+"&key=\t\n" +"0d8b8ecd019e414eb9ce8d667e8a5fa3";
//            String weatherUrl = "http://guolin.tech/api/weather?cityid=" +weatherId + "&key=0d8b8ecd019e414eb9ce8d667e8a5fa3";
            HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                String responseText=response.body().string();

                Weather weather=handleWeatherResponse(responseText);

                            if((weather != null) && "ok".equals(weather.getHeWeather6().get(0).getStatusX()))
                            {
                                SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();

                                editor.putString("BytezeroWeather",responseText);
                                editor.apply();

                            }
                        }

                    });
                }
        }


    private void updateWeather() {
        final String requestBingPic="http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String bingPic=response.body().string();
                SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                editor.putString("bing_pic",bingPic);
                editor.apply();
            }
        });

    }
}
